package com.work.studio.utils;


import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class TimerUtils {


    /**
     * 获取当前日期是星期几
     *
     * @param dateTime
     * @return
     */
    public static int getDayofWeek(String dateTime) {
        Calendar cal = Calendar.getInstance();
        if (dateTime.equals("")) {
            cal.setTime(new Date(System.currentTimeMillis()));
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            Date date;
            try {
                date = sdf.parse(dateTime);
            } catch (ParseException e) {
                date = null;
                e.printStackTrace();
            }
            if (date != null) {
                cal.setTime(new Date(date.getTime()));
            }
        }
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 获取当前日期是星期几
     *
     * @param dateTime
     * @return
     */
    public static int getDayofWeek(Date dateTime) {
        Calendar cal = Calendar.getInstance();

        if (dateTime != null) {
            cal.setTime(new Date(dateTime.getTime()));
        }
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    public static String StringDataWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
//        String mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
//        String mMonth = String.valueOf(c.get(Calendar.MONTH) +1);// 获取当前月份
//        String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return "星期"+mWay;
    }

    public static String StringDataNo(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
//        String mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
//        String mMonth = String.valueOf(c.get(Calendar.MONTH) +1);// 获取当前月份
        String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
//        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
//        if("1".equals(mWay)){
//            mWay ="天";
//        } else if("2".equals(mWay)){
//            mWay ="一";
//        } else if("3".equals(mWay)){
//            mWay ="二";
//        } else if("4".equals(mWay)){
//            mWay ="三";
//        } else if("5".equals(mWay)){
//            mWay ="四";
//        } else if("6".equals(mWay)){
//            mWay ="五";
//        } else if("7".equals(mWay)){
//            mWay ="六";
//        }
        return mDay;
    }


    /**
     * 获取当前日期是星期几
     *
     * @param dateTime
     * @return
     */
    public static int getDay(Date dateTime) {
        Calendar cal = Calendar.getInstance();

        if (dateTime != null) {
            cal.setTime(new Date(dateTime.getTime()));
        }
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 得到几天前的时间
     *
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return now.getTime();
    }

    /**
     * 获取当前时间以前7天时间
     *
     * @return
     */
    public static String getDateBeforeWeek() {
        Calendar now = Calendar.getInstance();
        now.setTime(getCurrentDateYMD());
        now.set(Calendar.DATE, now.get(Calendar.DATE) - 7);
        return dateToString(now.getTime());
    }

    /**
     * 获取当前时间以前30天时间
     *
     * @return
     */
    public static String getDateBeforeMonth() {
        Calendar now = Calendar.getInstance();
        now.setTime(getCurrentDateYMD());
        now.set(Calendar.DATE, now.get(Calendar.DATE) - 30);
        return dateToString(now.getTime());
    }

    public static Date StringToDate(String str) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    public static String dateToString(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
        String str = null;
        try {
            str = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return str;
    }

    /**
     * 字符串日期转成年月日
     *
     * @param str
     * @return
     */
    public static String strToYMD(String str) {


        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//小写的mm表示的是分钟

        try {
            Date date = sdf.parse(str);

            sdf = new SimpleDateFormat("MM/dd");

            result = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    public static String dateToStringMD(String str) {


        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//小写的mm表示的是分钟

        try {
            Date date = sdf.parse(str);

            sdf = new SimpleDateFormat("MM/dd");

            result = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


    public static String dateToStringHM(String str) {


        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//小写的mm表示的是分钟

        try {
            Date date = sdf.parse(str);

            sdf = new SimpleDateFormat("HH:mm");

            result = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    public static Date getCurrentDateYMD() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
        Date date = null;
        try {
            date = sdf.parse(getCurrentYMDTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    /**
     * 获得当前日期 yyyy-MM-dd HH:mm:ss
     *
     * @return 2019-08-27 14:12:40
     */
    public static String getCurrentYMDHMSTime() {
        // 小写的hh取得12小时，大写的HH取的是24小时
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        return df.format(date);
    }
    public static String getCurrentHMSTime() {
        // 小写的hh取得12小时，大写的HH取的是24小时
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 获得当前日期 yyyy-MM-dd HH:mm:ss
     *
     * @return 2019-08-27 14:12:40
     */
    public static String getCurrentYMD() {
        // 小写的hh取得12小时，大写的HH取的是24小时
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 获取年月日
     *
     * @return
     */
    public static String getCurrentYMDTime() {
        // 小写的hh取得12小时，大写的HH取的是24小时
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        return df.format(date);
    }

    public static String dealDateFormat(String oldDate) {
        Date date1 = null;
        DateFormat df2 = null;
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date date = df.parse(oldDate);
            SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
            date1 = df1.parse(date.toString());
            df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return df2.format(date1);
    }

    public static long getNowDistanceMill(Date oldDate, Date newDate) {

        long now = 0;

        try {

            long time = newDate.getTime() - oldDate.getTime();
            now = time;

        } catch (Exception e) {
            e.printStackTrace();
        }


        return now;
    }

    public static Date dealDate(String oldDate) {
        Date date1 = null;
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date date = df.parse(oldDate);
            SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
            date1 = df1.parse(date.toString());
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return date1;
    }

    public static String dealDateToStr(String oldDate) {
        if (TextUtils.isEmpty(oldDate)) {
            return "";
        }
        String value = "";
        Date date1 = null;
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date date = df.parse(oldDate);
            SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
            date1 = df1.parse(date.toString());

            DateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            value = dft.format(date1);
        } catch (ParseException e) {

            e.printStackTrace();
            return "";
        }
        return value;
    }


    public static String getDatePoor(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;

        long nh = 1000 * 60 * 60;

        long nm = 1000 * 60;

        // long ns = 1000;

        // 获得两个时间的毫秒时间差异

        long diff = endDate.getTime() - nowDate.getTime();

        // 计算差多少天

        long day = diff / nd;

// 计算差多少小时

        long hour = diff % nd / nh;

// 计算差多少分钟

        long min = diff % nd % nh / nm;

// 计算差多少秒//输出结果

// long sec = diff % nd % nh % nm / ns;

        return day + "天" + hour + "小时" + min + "分钟";

    }

    public static String getShowTime(long diff) {

        long nd = 1000 * 24 * 60 * 60;

        long nh = 1000 * 60 * 60;

        long nm = 1000 * 60;

        // long ns = 1000;

        // 获得两个时间的毫秒时间差异


        // 计算差多少天

        long day = diff / nd;

// 计算差多少小时

        long hour = diff % nd / nh;

// 计算差多少分钟

        long min = diff % nd % nh / nm;

// 计算差多少秒//输出结果

// long sec = diff % nd % nh % nm / ns;

        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static String getGapTime(long time) {
        long hours = time / (1000 * 60 * 60);
        long minutes = (time - hours * (1000 * 60 * 60)) / (1000 * 60);
        long second = (time - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
        String diffTime = "";
        if (minutes < 10) {
            diffTime = hours + ":0" + minutes;
        } else {
            diffTime = hours + ":" + minutes;
        }
        if (second < 10) {
            diffTime = diffTime + ":0" + second;
        } else {
            diffTime = diffTime + ":" + second;
        }
        return diffTime;
    }

    /**
     * 比较两个日期的大小，日期格式为yyyy-MM-dd
     *
     * @param str1 the first date
     * @param str2 the second date
     * @return true <br/>false
     */
    public static boolean isDate2Bigger(String str1, String str2) {
        boolean isBigger = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date dt1 = null;
        Date dt2 = null;
        try {
            dt1 = sdf.parse(str1);
            dt2 = sdf.parse(str2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (dt1.getTime() > dt2.getTime()) {
            isBigger = false;
        } else if (dt1.getTime() < dt2.getTime()) {
            isBigger = true;
        }
        return isBigger;
    }

    private static String covnDate(String dateTime) {
        DateFormat df2 = null;
        Date date1 = null;
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date date = df.parse(dateTime);
            SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
            date1 = df1.parse(date.toString());
            df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return df2.format(date1);
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @author jqlin
     */

    public static boolean isEffectiveDate(String startTime, String endTime) {
        boolean runFlag = false;
        String format = "HH:mm:ss";
        SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
        String now = sf.format(new Date());

        try {
            Date nowTimeDate = new SimpleDateFormat(format).parse(now);
            Date startTimeDate = new SimpleDateFormat(format).parse(startTime);
            Date endTimeDate = new SimpleDateFormat(format).parse(endTime);

            if (isEffectiveDateStr(nowTimeDate, startTimeDate, endTimeDate)) {
                runFlag = true;
//                logger.info("系统时间在早上9点到下午17点之间.");
            } else {
                runFlag = false;
//                logger.info("系统时间不在早上9点到下午17点之间.");
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return runFlag;

    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDateStr(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    public static String getCurrentTime() {

        String value = "";

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");

        value = formatter.format(date);
        return value;
    }

    /**

         * 获取两个时间段的分钟差

         *

         * @param startDate 年月日时分秒

         * @param endDate

         * @return

         */

//    public static int getGapMinutes(String startDate, String endDate) {
//
//        long start = 0;
//        long end = 0;
//        int minutes = 0;
//        LogUtils.e("开始结束时间" + startDate + "  " + endDate);
//
//        try {
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            start = df.parse(startDate).getTime();
//            end = df.parse(endDate).getTime();
//            minutes = (int) ((end - start) / (1000 * 60));
//            LogUtils.e("开始结束时间" + minutes + "");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return minutes;
//    }





}
